﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using System.Xml.Linq;
using XCommunity.Data;
using XCommunity.Documents;
using XCommunity.Frames;
using XCommunity.Users;

namespace XCommunity.Forums
{
  public class ForumsBoardFrame : IBoardFrame
  {


    public ForumsBoardFrame( ForumsSystem forumsSystem, XElement configElement )
    {
      Forums = forumsSystem;

      BoardId = configElement.Attribute( "boardId" ).Value;

      Moderators = configElement.Elements( ForumsSystem.XF + "moderator" ).Select( item => LoadModerator( item ) ).ToArray();
    }



    public string BoardId
    {
      get;
      private set;
    }



    protected FrameService Service
    {
      get { return Forums.Context.FrameService; }
    }

    protected ForumsSystem Forums
    {
      get;
      private set;
    }


    public int GetTopicNumber()
    {
      return Convert.ToInt32( ForumsDataProvider.DbUtility.ExecuteScalar( "SELECT COUNT( TopicId ) FROM ForumsBoardTopics WHERE BoardId = {0}", BoardId ) );
    }

    public int GetPostNumber()
    {
      if ( GetTopicNumber() == 0 )
        return 0;

      return GetTopics().Sum( topic => topic.Responses.Count() + 1 );

      //return Convert.ToInt32( ForumsDataProvider.DbUtility.ExecuteScalar( "SELECT SUM( Posts ) FROM ForumsBoardTopics WHERE ForumsBoardTopics.BoardId = {0}", BoardId ) );
    }




    public IEnumerable<ITopicFrame> GetTopics()
    {
      return ForumsDataProvider.DbUtility.ExecuteSingleColumn<int>( "SELECT TopicId FROM ForumsBoardTopics WHERE BoardId = {0}", BoardId ).Select( topicId => Service.GetTopicFrame( topicId ) );
    }


    /// <summary>
    /// 在版面上发起一个话题
    /// </summary>
    /// <param name="document"></param>
    /// <returns></returns>
    public GeneralTopicFrame LauchTopic( Document document )
    {
      return PublishTopic( Forums.LaunchTopic( document, this ) );
    }


    /// <summary>
    /// 发布一个话题到该版面
    /// </summary>
    /// <param name="topic"></param>
    /// <returns></returns>
    public GeneralTopicFrame PublishTopic( GeneralTopicFrame topic )
    {
      ForumsDataProvider.DbUtility.ExecuteNonQuery( "INSERT INTO ForumsBoardTopics ( TopicId, BoardId, PublishDate ) VALUES ( {...} )", topic.TopicId, BoardId, DateTime.UtcNow );
      return topic;
    }



    public void DeleteTopic( GeneralTopicFrame topic )
    {
      ForumsDataProvider.DbUtility.ExecuteNonQuery( "UPDATE ForumsBoardTopics SET Deleted = 1 WHERE TopicId = {0} AND BoardId = {1}", topic.TopicId, BoardId );
    }



    protected virtual IModerator LoadModerator( XElement configElement )
    {
      string username = configElement.Attribute( "username" ).Value;

      IControlItem[] permissions = null;//Forums.UserService.LoadControlGroup( configElement.Element( ForumsSystem.XF + "permissions" ) ).ToArray();

      return new Moderator( username, this, permissions );
    }


    /// <summary>
    /// 获取板块的管理员
    /// </summary>
    public IModerator[] Moderators
    {
      get;
      private set;
    }

  }
}
